﻿using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.CustomHelpers;
using KMF.WebUI.Areas.KMFadmin.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminProductCategoryMapLinkController : Controller
    {
        //
        // GET: /ProductCategoryMapLink/

        public ActionResult Index(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var model = ProductCategoryMapLinkDb.GetAllProductCategoryMapLinkPaging(page, itemsPerPage, ref totalPage) ?? new List<ProductCategoryMapLinkPagingDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.MenuList = FunctionHelper.GetMenuListThreeLevel() ?? new List<ProductCategoryDto>();
            return View(model);
        }

        public ActionResult ShowByCate(string category)
        {
            var model = ProductCategoryMapLinkDb.ShowBYCate(category) ?? new List<ProductCategoryMapLinkDto>();
            ViewBag.MenuList = FunctionHelper.GetMenuListThreeLevel() ?? new List<ProductCategoryDto>();
            ViewBag.CategoryId = category;
            return View(model);
        }
        public ActionResult Add()
        {
            var model = new ProductCategoryMapLinkDto();
            model.Active = true;
            ViewBag.MenuList = FunctionHelper.GetMenuListThreeLevel() ?? new List<ProductCategoryDto>();
            return View(model);
        }
        [HttpPost]
        public ActionResult Add(ProductCategoryMapLinkDto model)
        {
            var result = ProductCategoryMapLinkDb.Add(model);
            if (result > 0)
                return RedirectToAction("Index", "AdminProductCategoryMapLink");
            return View(model);

        }

        public ActionResult Edit(int id)
        {
            var model = ProductCategoryMapLinkDb.GetById(id) ?? new ProductCategoryMapLinkDto();
            ViewBag.MenuList = FunctionHelper.GetMenuListThreeLevel() ?? new List<ProductCategoryDto>();
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(ProductCategoryMapLinkDto model)
        {
            ProductCategoryMapLinkDb.Update(model);
            return RedirectToAction("Index", "AdminProductCategoryMapLink");
        }
        public JsonResult DeleteProductCategoryMapLink(int id)
        {
            var data = 0;
            if (id!=0)
            {
                data = ProductCategoryMapLinkDb.Delete(id);
            }
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
